package com.vint.lintcode;

import com.vint.common.ListNode;

import java.util.HashSet;
import java.util.Set;

/**
 * 链表有多少个节点，考虑环的情况
 */
public class Lintcode466 {
	public int countNodes(ListNode head) {
		Set<ListNode> sets = new HashSet<>();
		int count = 0;
		while (null!=head&&!sets.contains(head)){
			sets.add(head);
			count++;
			head = head.next;
		}
		return count;
	}
	public static void main(String[] args) {
		ListNode r1 = new ListNode(3);
		ListNode r2 = new ListNode(3);
		ListNode r3 = new ListNode(3);
		r1.next = r2;
		r2.next = r3;
		r3.next = r1;
		Lintcode466 lintcode466 = new Lintcode466();
		System.out.println(lintcode466.countNodes(r1));

	}
}
